/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.nonmember;

import entity.Product;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Paginator;
import service.ProductServiceLocal;

/**
 *
 * @author SONY
 */
public class ListProductServlet extends HttpServlet {
    
    @EJB ProductServiceLocal productServiceLocal;
    
    static int N = 0;
    static int n = 0;
    static int nPages = 0;
    static int currentpage = 0; 

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        ServletContext context = this.getServletContext();
        productServiceLocal = (ProductServiceLocal) context.getAttribute("productServiceLocal");
        List<Product> lst= productServiceLocal.getAllProducts();
        n = lst.size();
        System.out.println("n " + n);

        N = 6;
        if( n % N == 0) {
            nPages = (int)n/N;
            System.out.println("nPages_0 " + nPages);

        } else {	
            nPages = (int)Math.ceil((double)(n)/N);
            System.out.println("nPages_1 " + ((double)(n)/N) + " : " + nPages);

        }

        String current = request.getParameter("currentpage");
        if(current == null){
            currentpage = 0;
        } else {
            currentpage = Integer.parseInt(current);
        }

        System.out.print("current page " + currentpage);

        HttpSession session = request.getSession();
        session.setAttribute("nPages", nPages);
        session.setAttribute("currentpage", currentpage);

        //call sublist
        Paginator paging = new Paginator();
        List<Product> sublist=paging.getSubList(currentpage, N, n, lst);
        session.setAttribute("lstproducts", sublist);
        response.sendRedirect("list_products.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
